﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Parser;

namespace Changer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private NetworkPerson[] source = null;
        private NetworkPerson[] sourceInit = null;
        private string path = "f:\\odnoklassniki\\";
        private NetworkPerson personToEdit = null;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            InitItems();
        }

        private void InitItems()
        {
            var p = NetworkPerson.GetPersons(path);
            dataGrid1.ItemsSource = p;
            source = p;
            sourceInit = NetworkPerson.GetPersons(path);
        }

        private void dataGrid1_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            var idx = GetIndexOfItem(personToEdit);
            if (idx == -1)
                return;
            var prev = sourceInit.FirstOrDefault(itm => itm.ShowName == personToEdit.ShowName);
            if (prev == null)
                return;
            if (prev.FullName.Trim() == personToEdit.FullName.Trim())
                return;
            //if (MessageBox.Show("Do you really want to edit item?", "Warning", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            //{
            source[idx].Name = personToEdit.Name;
            source[idx].SurName = personToEdit.SurName;
            source[idx].MaidenName = personToEdit.MaidenName;
            source = source.Where(itm => itm != null && !string.IsNullOrWhiteSpace(itm.FullName)).OrderBy(itm => itm.FullName).ToArray();
            NetworkPerson.SavePersons(path, source);
            dataGrid1.ItemsSource = source;
            prev.ChangePersonsPicture(path, personToEdit.FullName);
            sourceInit = NetworkPerson.GetPersons(path);
                //return;
            //}
            //e.Cancel = true;
        }

        private string GetValue(int index, DataGridRow row)
        {
            var element = dataGrid1.Columns[index].GetCellContent(row) as TextBox;
            if (element != null)
                return element.Text;
            var elementB = dataGrid1.Columns[index].GetCellContent(row) as TextBlock;
            return elementB != null ? elementB.Text : string.Empty;
        }

        private int GetIndexOfItem(NetworkPerson item)
        {
            var idx = -1;
            foreach (var networkPerson in source)
            {
                idx++;
                if (networkPerson.ShowName == item.ShowName)
                    break;
            }
            if (idx == source.Length - 1 && source[idx].ShowName != item.ShowName)
                idx = -1;
            return idx;
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            personToEdit = dataGrid1.SelectedItem as NetworkPerson;
        }

        private void dataGrid1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            personToEdit.Name = GetValue(4, e.Row);
            personToEdit.SurName = GetValue(3, e.Row);
            personToEdit.MaidenName = GetValue(5, e.Row);
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (personToEdit == null)
                return;
            var idx = GetIndexOfItem(personToEdit);
            if (idx == -1)
                return;
            var prev = sourceInit.FirstOrDefault(itm => itm.ShowName == personToEdit.ShowName);
            if (prev == null)
                return;
            source = source.Where(itm => itm != null && !string.IsNullOrWhiteSpace(itm.FullName)).Where(itm => itm.ShowName != personToEdit.ShowName).OrderBy(itm => itm.FullName).ToArray();
            NetworkPerson.SavePersons(path, source);
            dataGrid1.ItemsSource = source;
            prev.DeletePersonsPicture(path);
            sourceInit = NetworkPerson.GetPersons(path);
        }

        private void btnUrl_Click(object sender, RoutedEventArgs e)
        {
            if (personToEdit == null)
                return;
           Clipboard.SetText(personToEdit.OdnoklassnikiSite);
        }

        private void btnDate_Click(object sender, RoutedEventArgs e)
        {
            if (personToEdit == null || !personToEdit.Birthday.HasValue)
                return;
            Clipboard.SetText(personToEdit.Birthday.Value.ToString("dd.MM.yyyy"));
        }

    }
}
